import csv
import os
import pickle
import shutil
from tkinter import filedialog
import pandas as pd
from openpyxl import workbook

def import_pkl_from_excel(domain, data_type):
    # ————————————————————写入到csv文件

    output_dir='output/dataset_csv'
    split_colume = 'isIDESession'
    sort_colume = 'kave_triggeretime'
    relation_colume = '关系描述'

    df = pd.read_excel(f"data_xlsx/{data_type}.xlsx")
    split_values = df[split_colume].unique()

    if os.path.exists(output_dir):
        shutil.rmtree(output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    for i,value in enumerate(split_values):
        filename = f"{output_dir}/{domain}-{data_type}_pkl_output-{i}.csv"
        with open(filename, 'w', newline='') as csv_file:
            # 创建一个csv.writer对象
            writer = csv.writer(csv_file)
            headers=['time','name']
            fistln=[0.0,'bos']
            writer.writerow(headers)
            writer.writerow(fistln)
        df[df[split_colume] == value][[sort_colume,relation_colume]].to_csv(filename, index=False, mode='a', header=False)
        with open(filename, 'a', newline='') as csv_file:
            # 创建一个csv.writer对象
            writer = csv.writer(csv_file)
            trgger_time_max = df[df[split_colume] == value][sort_colume].max()
            lastln=[trgger_time_max + 1,'eos']
            writer.writerow(lastln)
        print('has wrote :',filename)



    # ——————————————————写入到pkl文件

    list_result = []

    # 步骤 1: 读取 CSV 文件
    i = 0
    csv_name = f'output/dataset_csv/{domain}-{data_type}_pkl_output-{i}.csv'
    csv_exist = os.path.exists(csv_name)
    while csv_exist:
        csv_data = []
        with open(csv_name, 'r') as file:
            reader = csv.DictReader(file)
            for row in reader:
                # 假设每行只有一个数据点，添加第一个元素到列表
                row['time'] = float(row['time'])
                csv_data.append(row)
            list_result.append(csv_data)
        i+=1
        csv_name = f'output/dataset_csv/{domain}-{data_type}_pkl_output-{i}.csv'
        csv_exist = os.path.exists(csv_name)
        print(f"csv {csv_name} exist: {csv_exist}")

    output_dir = f'output/{domain}'
    # 检查output文件夹是否存在，不存在则创建
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    pkl_name = f'{output_dir}/{data_type}.pkl'
    # 步骤 2: 使用 pickle 序列化数据
    with open(pkl_name, 'wb') as file:
        pickle.dump(list_result, file)


    print(f"{domain}-{data_type} CSV Data has been written to {pkl_name}")




domain  = 'devevent'
data_type = 'train'
import_pkl_from_excel(domain, data_type)
data_type = 'test'
import_pkl_from_excel(domain, data_type)
data_type = 'dev'
import_pkl_from_excel(domain, data_type)